﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LeSoft.Application.Bussiness;
using LeSoft.Application.Code;
using LeSoft.Application.Entity;
using LeSoft.Util;
using Newtonsoft.Json;

namespace LeSoft.Application.Web.Areas.SystemManage.Controllers
{
    public class BussinessIndicatorController : MvcControllerBase
    {
        BussinessIndicatorRuleBLL ruleBll = new BussinessIndicatorRuleBLL();
        BussinessIndicatorBLL Bll = new BussinessIndicatorBLL();
        //
        // GET: /SystemManage/BussinessIndicator/
        #region 视图功能

        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        public ActionResult Index()
        {
            return View();
        }


        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        public ActionResult RuleIndex()
        {
            return View();
        }


        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        public ActionResult RuleForm()
        {
            return View();
        }

        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        public ActionResult ValueForm()
        {
            return View();
        }

        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        public ActionResult Icon()
        {
            return View();
        }
        #endregion

        #region 验证数据
        /// <summary>
        /// 编码不能重复
        /// </summary>
        /// <param name="Code">编号</param>
        /// <param name="keyValue">主键</param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ExistRuleCode(string Code, string keyValue)
        {
            bool IsOk = ruleBll.ExistCode(Code, keyValue);
            return Content(IsOk.ToString());
        }
        /// <summary>
        /// 名称不能重复
        /// </summary>
        /// <param name="Name">名称</param>
        /// <param name="keyValue">主键</param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ExistRuleName(string Name, string keyValue)
        {
            bool IsOk = ruleBll.ExistName(Name, keyValue);
            return Content(IsOk.ToString());
        }
        #endregion

        #region 查询数据
        public ActionResult GetRuleEntity(string keyValue)
        {
            var data = ruleBll.GetEntity(keyValue);
            return Content(data.ToJson());
        }
        public ActionResult GetRuleList(Pagination pagination, string condition, string keyword)
        {
            var watch = CommonHelper.TimerStart();
            var data = ruleBll.GetPageList(pagination, condition, keyword); 
            var JsonData = new
            {
                rows = data,
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                costtime = CommonHelper.TimerEnd(watch)
            };
            return Content(JsonData.ToJson());
        }

        public ActionResult GetList(Pagination pagination, string queryJson)
        {
            var watch = CommonHelper.TimerStart();
            var dataList = Bll.GetPageList(pagination, queryJson).Join(ruleBll.GetList(), x => x.RuleId, y => y.Id, (x, y) => new
            {
                x.Id,
                x.CountUserName,
                x.Date,
                x.Month,
                x.Time,
                x.Value,
                x.Year,
                x.RuleName,
                y.ValueFlag,
                Color = GetColor(x, y)
            }).ToList();

            var data = dataList;
            var JsonData = new
            {
                rows = data,
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                costtime = CommonHelper.TimerEnd(watch)
            };
            return Content(JsonData.ToJson());
        }

        public string GetColor(BussinessIndicatorEntity x, BussinessIndicatorRuleEntity y)
        {
            var obj = new[] { new { Id = "", Flag = "", ValueMemo = "", MinValue = "1", MaxValue = "", Color = "" } };
            obj = JsonConvert.DeserializeAnonymousType(y.ValueFlag, obj);
            foreach (var item in obj)
            {
                switch (item.Flag)
                {
                    case "0": if (!string.IsNullOrEmpty(item.MinValue)) { if (Convert.ToDouble(x.Value) < Convert.ToDouble(item.MinValue)) { return item.Color; } } else { if (Convert.ToDouble(x.Value) > Convert.ToDouble(item.MaxValue)) { return item.Color; } } break;
                    case "1": if (!string.IsNullOrEmpty(item.MinValue)) { if (Convert.ToDouble(x.Value) <= Convert.ToDouble(item.MinValue)) { return item.Color; } } else { if (Convert.ToDouble(x.Value) >= Convert.ToDouble(item.MaxValue)) { return item.Color; } } break;
                    case "2": if (!string.IsNullOrEmpty(item.MinValue)) { if (Convert.ToDouble(x.Value) == Convert.ToDouble(item.MinValue)) { return item.Color; } } else { if (Convert.ToDouble(x.Value) == Convert.ToDouble(item.MaxValue)) { return item.Color; } } break;
                    case "3": if (!string.IsNullOrEmpty(item.MinValue)) { if (Convert.ToDouble(x.Value) >= Convert.ToDouble(item.MinValue)) { return item.Color; } } else { if (Convert.ToDouble(x.Value) <= Convert.ToDouble(item.MaxValue)) { return item.Color; } } break;
                    case "4": if (!string.IsNullOrEmpty(item.MinValue)) { if (Convert.ToDouble(x.Value) > Convert.ToDouble(item.MinValue)) { return item.Color; } } else { if (Convert.ToDouble(x.Value) < Convert.ToDouble(item.MaxValue)) { return item.Color; } } break;
                    case "5": if ((Convert.ToDouble(x.Value) >= Convert.ToDouble(item.MinValue) && Convert.ToDouble(x.Value) <= Convert.ToDouble(item.MaxValue)) || (Convert.ToDouble(x.Value) <= Convert.ToDouble(item.MinValue) && Convert.ToDouble(x.Value) >= Convert.ToDouble(item.MaxValue))) { return item.Color; } break;
                    default:
                        break;
                }
            }
            return "#cecece";
        }
        #endregion
        #region 保存数据
        public ActionResult SaveRuleForm(string keyValue, BussinessIndicatorRuleEntity bussinessIndicatorRuleEntity)
        {
            ruleBll.SaveForm(keyValue, bussinessIndicatorRuleEntity);
            return Success("操作成功。");
        }


        public ActionResult RemoveRuleForm(string keyValue)
        {
            ruleBll.RemoveForm(keyValue);
            return Success("操作成功。");
        }
        #endregion
    }
}
